<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('client.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('client.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('client.name')" prop="name">
                    <el-input v-model="dataForm.name" :placeholder="i18n('common.please_input') + i18n('client.name')" clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('client.level')" prop="level">
                    <el-select
                      v-model="dataForm.level"
                      :placeholder="i18n('common.please_select') + i18n('client.level')" clearable>
                      <el-option v-for="(v, k) in levelList" :label="v.text" :key="k" :value="v.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('client.scale')" prop="scale">
                    <el-select
                      v-model="dataForm.scale"
                      :placeholder="i18n('common.please_select') + i18n('client.scale')" clearable>
                      <el-option v-for="(v, k) in scaleList" :label="v.text" :key="k" :value="v.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('client.industry_id')" prop="industry_id">
                    <el-cascader v-model="dataForm.industry_id" :placeholder="$t('common.please_select')+$t('example.industry_id')" :options="industryList" :props="levelProps" clearable></el-cascader>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('client.contact_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('client.contact_name')" prop="contact_name">
                    <el-input v-model="dataForm.contact_name" :placeholder="i18n('common.please_input') + i18n('client.contact_name')" clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('client.contact_mobile')" prop="contact_mobile">
                    <el-input v-model="dataForm.contact_mobile" :placeholder="i18n('common.please_input') + i18n('client.contact_mobile')" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('client.contact_position')" prop="contact_position">
                    <el-input v-model="dataForm.contact_position" :placeholder="i18n('common.please_input') + i18n('client.contact_position')" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('client.contact_sex')" prop="contact_sex">
                    <el-select
                      v-model="dataForm.contact_sex"
                      :placeholder="i18n('common.please_select') + i18n('client.contact_sex')" clearable>
                      <el-option v-for="(v, k) in sexList" :label="v.text" :key="k" :value="v.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('client.demand_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('client.is_demand')" prop="is_demand">
                    <el-radio-group v-model="dataForm.is_demand" @change="inputDemand">
                      <el-radio label="1">填写需求信息</el-radio>
                      <el-radio label="2">暂不填写</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('demand.demand_type')" prop="demand_type">
                    <el-select
                      v-model="dataForm.demand_type"
                      :disabled="status"
                      :placeholder="i18n('common.please_select') + i18n('demand.demand_type')" clearable>
                      <el-option v-for="(v, k) in demandTypeList" :label="v.text" :key="k" :value="v.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('demand.demand_time')" prop="demand_time">
                    <el-date-picker v-model="dataForm.demand_time" type="month" :disabled="status" :placeholder="i18n('common.please_input') + i18n('demand.demand_time')" clearable/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item :label="i18n('demand.description')" prop="description">
                    <el-input
                      type="textarea"
                      v-model="dataForm.description"
                      :rows="3"
                      :disabled="status"
                      show-word-limit
                      maxlength="500"
                      :placeholder="i18n('common.please_input') + i18n('client.description')">
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="client-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/client')

  const levelProps = ref({
    label: 'title',
    value: 'id',
    emitPath: false,
    checkStrictly: false
  })

  const status = ref(true)
  const levelList = ref([])
  const scaleList = ref([])
  const industryList = ref([])
  const sexList = ref([])
  const demandTypeList = ref([])

  const dataForm = reactive({
    id: 0,
    name: '',
    level: '',
    scale: '',
    industry_id: '',
    contact_name: '',
    contact_sex: '',
    contact_mobile: '',
    contact_position: '',
    is_demand: '2',
    demand_type: '',
    demand_time: '',
    description: '',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    name: [{
      required: true,
      message: i18n('client.rules.name.require'),
      trigger: 'blur',
    }],
    level: [{
      required: true,
      message: i18n('client.rules.level.require'),
      trigger: 'blur',
    }],
    scale: [{
      required: true,
      message: i18n('client.rules.scale.require'),
      trigger: 'blur',
    }],
    sex: [{
      required: true,
      message: i18n('client.rules.sex.require'),
      trigger: 'blur',
    }],
    mobile: [{
      required: true,
      message: i18n('client.rules.mobile.require'),
      trigger: 'blur',
    }],
    industry_id: [{
      required: true,
      message: i18n('client.rules.industry_id.require'),
      trigger: 'blur',
    }],
    contact_name: [{
      required: true,
      message: i18n('client.rules.contact_name.require'),
      trigger: 'blur',
    }],
    contact_sex: [{
      required: true,
      message: i18n('client.rules.contact_sex.require'),
      trigger: 'blur',
    }],
    contact_mobile: [{
      required: true,
      message: i18n('client.rules.contact_mobile.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.name = data.data.name
            dataForm.level = data.data.level.value + ''
            dataForm.scale = data.data.scale.value + ''
            dataForm.industry_id = data.data.industry_id
            dataForm.contact_name = data.data.contact_name
            dataForm.contact_sex = data.data.contact_sex
            dataForm.contact_mobile = data.data.contact_mobile
            dataForm.contact_position = data.data.contact_position
            dataForm.is_demand = data.data.is_demand + ''
            dataForm.demand_type = data.data.demand_type
            dataForm.demand_time = data.data.demand_time
            dataForm.description = data.data.description
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''

            if(1 == dataForm.is_demand) {
              status.value = false
            }
          }
        })
      }
    })
  }


  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            name: dataForm.name,
            level: dataForm.level,
            scale: dataForm.scale,
            industry_id: dataForm.industry_id,
            contact_name: dataForm.contact_name,
            contact_sex: dataForm.contact_sex,
            contact_mobile: dataForm.contact_mobile,
            contact_position: dataForm.contact_position,
            is_demand: dataForm.is_demand,
            demand_type: dataForm.demand_type,
            demand_time: dataForm.demand_time,
            description: dataForm.description,
            sort: dataForm.sort,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }


  const inputDemand = (value) => {
    if(1 == value) {
      status.value = false
    } else {
      status.value = true
    }
  }


  const loadLevelList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'client_level',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        levelList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  const loadScaleListList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'scale_level',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        scaleList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadIndustryList = () => {
    $http({
      url: $http.adornUrl('/industry/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        industryList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  const loadSexList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'user_sex_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        sexList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  const loadDemandTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'demand_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        demandTypeList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadLevelList()
    loadScaleListList()
    loadIndustryList()
    loadSexList()
    loadDemandTypeList()
  })
</script>
